package com.gzgy.ustore.api.dao.impl;

import com.gzgy.ustore.api.common.jdbc.StatementParameter;
import com.gzgy.ustore.api.dao.UserSessionDao;
import com.ustore.model.UserSession;
import org.springframework.stereotype.Repository;

@Repository
public class UserSessionDaoImpl  extends  AbstractBaseDao<UserSession> implements UserSessionDao{
    @Override
    public UserSession getSessionByUserId(Integer userId, String sessionId) {

        String sql ="SELECT * FROM "+this.tableName+" WHERE USER_ID = ? AND SESSION =?";
        StatementParameter parameter = new StatementParameter();
        parameter.setInt(userId);
        parameter.setString(sessionId);
        return this.query(sql,UserSession.class,parameter);
    }

    @Override
    public Integer addUserSession(Integer userId, String session) {
        StatementParameter parameter = new StatementParameter();
        parameter.setInt(userId);
        parameter.setString(session);
        parameter.setString(userId+"");
        parameter.setString(userId+"");
        StringBuffer sb = new StringBuffer();
        sb.append("REPLACE INTO user_session(USER_ID,SESSION,CREATED_TIME,CREATED_BY,UPDATED_TIME,UPDATED_BY)");
        sb.append("VALUES(?,?,now(),?,now(),?)");
        return this.updateForRecord(sb.toString(),parameter);
    }
}
